package com.eoe.wiki.activity;

import com.eoe.wiki.utils.WikiUtil;
import com.eoe.wiki.view.SliderLayer;
import com.eoe.wiki.view.SliderLayer.SliderListener;

import android.os.Bundle;
import android.view.MotionEvent;


public abstract class SliderActivity extends BaseActivity implements SliderListener {

	public static final String KEY_SLIDER_INDEX = "slider_index";
	protected MainActivity mMainActivity;
	protected int mSliderIndex;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mMainActivity = getWikiApplication().getMainActivity();
		if(mMainActivity==null){
			throw new NullPointerException("you should start MainActivity firstly.");
		}
		mSliderIndex = getIntent().getIntExtra(KEY_SLIDER_INDEX, 0);
		mMainActivity.getSliderLayer().addSliderListener(this);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mMainActivity.getSliderLayer().removeSliderListener(this);
		super.onDestroy();
		
	}
	
	public void closeSlider(){
		SliderLayer layer = mMainActivity.getSliderLayer();
		layer.closeSidebar(layer.openingLayerIndex());
	}
	
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(mSliderIndex!=mMainActivity.getSliderLayer().openingLayerIndex()){
			return false;
		}
		return super.dispatchTouchEvent(ev);
	}
	@Override
	public void sliderbarOpened() {
		// TODO Auto-generated method stub
		WikiUtil.hideSoftInput(getWindow().getDecorView());
		onSliderbaropened();
		mMainActivity.getSliderLayer().removeSliderListener(this);
	}
	@Override
	public void sliderbarClosed() {
		// TODO Auto-generated method stub
		onSliderbarClosed();
		mMainActivity.getSliderLayer().removeSliderListener(this);
	}
	@Override
	public boolean contentTouchedWhenOpening() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public abstract void onSliderbaropened();
	public abstract void onSliderbarClosed();
}
